/**
 * 银行卡设置
 */

define(function (require, exports, module) {

    var reg_idNumber=/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
    var reg_bankId = /^(\d{16}|\d{19})$/;


    require("jquery.cxselect")


    var UI = require("js/UI"),
        uiTip=require("ui/tip"),
        dropDownList=require("ui/dropDownList");


    var $homeCnt = $("#home .cnt");

    var $bankList = $homeCnt.find(".bankList");
    var $formA = $homeCnt.find(".formWrap form");    //添加银行卡表单

    var $formB = $homeCnt.find(".fromBox form");;    //修改银行卡表单

    var $bank_from = $("#addBank form");
    var $alipay_from = $("#addAlipay form");
    var $tenpay_from = $("#addTenpay form");


    //选择银行卡
    var $listWrap = $(".listWrap"),$formWrap = $(".formWrap");
    var $p = $listWrap.find("p");

    var $bank = $bankList.find(".bank").unbind("click.bank").bind("click.bank", function () {
        var $this = $(this), id = $this.attr("id");
        $bank.removeClass("curr");
        $this.addClass("curr");

        //修改银行卡
        if($this.hasClass("own")){
            $p.hide();
            $(".fromBox").show();

            if($this.hasClass("oBank")){
                UI.ajax("/json/bankinfor.json", "post", {bankId: id}, function (flag, json) {
                    var bankData=json;
                    var $bankForm=$(template("home/user/bank",bankData))
                    $formB.find(".formList").remove().end().find(".tit").after($bankForm);
                    formEventBind($bankForm.closest("form"));
                    $(".bankSelect .G_ipt").click(function(){
                        $(".bankSelect ul").hide();
                    });
                });
            }
            if($this.hasClass("oAlipay")){
                UI.ajax("/json/bankinfor.json", "post", {bankId: id}, function (flag, json) {
                    var bankData=json;
                    var $bankForm=$(template("home/user/alipay",bankData))
                    $formB.find(".formList").remove().end().find(".tit").after($bankForm);
                    formEventBind($bankForm.closest("form"));
                });

            }
            if($this.hasClass("oTenpay")){

                UI.ajax("/json/bankinfor.json", "post", {bankId: id}, function (flag, json) {
                    var bankData=json;
                    var $bankForm=$(template("home/user/tenpay",bankData))
                    $formB.find(".formList").remove().end().find(".tit").after($bankForm);
                    formEventBind($bankForm.closest("form"));
                });

            }

        }    //修改银行卡end


    })//选择银行卡end

    //添加银行卡、添加支付宝、添加财付通
    //添加银行卡

    var $add = $bankList.find(".add").unbind("click.bank").bind("mouseover.add",function(){
        $p.show();
    });
    var $a = $p.find("a").unbind("click.a").bind("click.a",function(){
        var $this = $(this);
        if($this.hasClass("oBank")){
            $listWrap.hide();
            $formWrap.hide();
            $("#addBank").show();
            var $bankForm=$(template("home/user/bank",{}))
            $formA.eq(0).find(".formList").remove().end().find(".tit").after($bankForm);
            var mobile = $("#bankMobile").val();
            $("#addBank .telNum").text(mobile);
            formEventBind($formA.eq(0));
        }
        if($this.hasClass("oAlipay")){
            $listWrap.hide();
            $formWrap.hide();
            $("#addAlipay").show();
            var $bankForm=$(template("home/user/alipay",{}))
            $formA.eq(1).find(".formList").remove().end().find(".tit").after($bankForm);
            var mobile = $("#bankMobile").val();
            $("#addAlipay .telNum").text(mobile);
            formEventBind($formA.eq(1));
        }
        if($this.hasClass("oTenpay")){
            $listWrap.hide();
            $formWrap.hide();
            $("#addTenpay").show();
            var $bankForm=$(template("home/user/tenpay",{}))
            $formA.eq(2).find(".formList").remove().end().find(".tit").after($bankForm);
            var mobile = $("#bankMobile").val();
            $("#addTenpay .telNum").text(mobile);
            formEventBind($formA.eq(2));
        }
    });
    $add.mouseout(function(){
        $p.hide();
    })

    // 设为默认账户
    $bank.find(".txt a.last").unbind("click.def").bind("click.def", function () {
        var $this = $(this), id = $this.attr("id");
        var $parentBank = $this.closest(".bank");
        UI.ajax("/User/Bankcard/defaultBank", "post", {bid: id}, function (flag, json) {
            $bank.removeClass("default");
            $parentBank.addClass("default");
        })
        return false
    })

    //删除账户
    $bank.find(".txt a.delete").unbind("click.delete").bind("click.delete", function () {
        var $this = $(this), id = $this.attr("id");
        var $parentBank = $this.closest(".bank");
        if($this.next("em").is(":visible")){
            UI.tooltip("error", "默认账户不能删除");
        }else{
            var tooltipInst=new UI.tooltip("submit", "您要删除银行卡？");
            tooltipInst.submitCall(function (){
                UI.ajax("/json/bankinfor.json", "post", {}, function (flag, json) {
                     $parentBank.remove();
                });
            })
        };
        return false
    })

    var formEventBind= function (_form) {
        var $bankSelect = _form.find(".bankSelect"),
            $placeSelect = _form.find("#placeSelect"),
            $name = _form.find(".name"),
            $idNumber = _form.find(".idNumber"),
            $openingBank = _form.find(".openingBank"),
            $bankId = _form.find(".bankId"),
            $telnum = _form.find(".telnum"),
            $payId = _form.find(".payId"),
            $telNum = _form.find(".telNum"),
            $telBtn = _form.find(".telBtn");


        //下拉选择
        UI.dropDownSelect($bankSelect);

        dropDownList({
            $province: _form.find(".provinceSelect"),
            $city: _form.find(".citySelect")
        })

        //手机验证码
        $telBtn.unbind("click.tel").bind("click.tel",function(){
            UI.ajax("/json/bankinfor.json", "post", {mobile: $telNum.text()}, function (flag, json) {
                //console.log(json.mobile);
                UI.tooltip("success", "默认账户不能删除");
            })

        });

        _form.find("button[type=submit]").unbind("click.sub").bind("click.sub", function () {

        var _name = $.trim($name.val()), _idNumber = $.trim($idNumber.val()),
            _openingBank = $.trim($openingBank.val()), _bankId = $.trim($bankId.eq(0).val()),
            _bankIds = $.trim($bankId.eq(1).val()),_telnum = $.trim($telnum.val()),_payId = $.trim($payId.eq(0).val()),
            _payIds = $.trim($payId.eq(1).val());

        if (!_name) {
            uiTip($name, "请输入真实姓名", "right").show();
            $name.focus();
            return false
        }
        if ($idNumber && $idNumber[0]) {
            if (!_idNumber) {
                    uiTip($idNumber, "请输入您的身份证号", "right").show();
                    $idNumber.focus();
                    return false
            }else{
               if(!_idNumber.match(reg_idNumber)){
                    uiTip($idNumber, "请输入正确的身份证号", "right").show();
                    $idNumber.focus();
                    return false
                }
            }
        }
        if($openingBank && $openingBank[0]){
            if (!_openingBank) {
                uiTip($openingBank, "请输入您的开户行", "right").show();
                $openingBank.focus();
                return false
            }
        }
        if($bankId && $bankId[0]){
            var $bankIdOne=$bankId.eq(0),$bankIdTwo=$bankId.eq(1);
            if (!_bankId) {
                uiTip($bankIdOne, "请输入您的银行卡号", "right").show();
                $bankIdOne.focus();
                return false
            }else{
                if(!_bankId.match(reg_bankId)){
                    uiTip($bankIdOne, "请输入正确格式的银行卡号", "right").show();
                    $bankIdOne.focus();
                    return false
                }
            }
            if (!_bankIds) {
                uiTip($bankIdTwo, "请再次输入银行卡号", "right").show();
                $bankId.eq(1).focus();
                return false
            } else {
                if (_bankId !== _bankIds) {
                    uiTip($bankIdTwo, "您输入的银行卡号不一致", "right").show();
                    $bankIdTwo.focus();
                    return false
                }
            }
        }
        if($payId && $payId[0]){
                var $payIdOne=$payId.eq(0),$payIdTwo=$payId.eq(1);
                if (!_payId) {
                    uiTip($payIdOne, "请输入您的支付帐号", "right").show();
                    $payIdOne.focus();
                    return false
                }
                if (!_payIds) {
                    uiTip($payIdTwo, "请再次输入支付帐号", "right").show();
                    $payId.eq(1).focus();
                    return false
                } else {
                    if (_payId !== _payIds) {
                        uiTip($payIdTwo, "您输入的支付帐号不一致", "right").show();
                        $payIdTwo.focus();
                        return false
                    }
                }
            }
        if (!_telnum) {
            uiTip($telnum, "请输入手机验证码", "right").show();
            $telnum.focus();
            return false
         }
    }) //保存按钮
    };
    //formEventBind();
});